<template>
	<view class="search-page">
		<!-- 状态栏占位 -->
		<view class="status-bar"></view>
		<!-- #ifdef MP-WEIXIN -->
		<headBarWX title="搜索" :show='true'></headBarWX>
		<!-- #endif -->
		<!-- #ifndef MP-WEIXIN -->
		<headBarAPP title='搜索'></headBarAPP>
		<!-- #endif -->

		<view class="container">
			<!-- 搜索框 -->
			<view class="searchBox">
				<text class="iconfont">&#xe6e4</text>
				<input type="text" placeholder="搜索歌曲" confirm-type="search" @confirm="search()" v-model="searchText" />
			</view>
			<!-- 按钮 搜索什么内容 -->
			<view class="btn-category">
				<view class="item btn-search-showMusic" :class="{'activate':status.search_showMusic}"
					@tap="search_showMusic()">歌曲</view>
				<view class="item btn-search-showAlbum" :class="{'activate':status.search_showAlbum}"
					@tap="search_showAlbum()">歌单</view>
				<view class="item btn-search-showAuthor" :class="{'activate':status.search_showAuthor}"
					@tap="search_showAuthor()">歌手
				</view>
			</view>
			<!-- 显示 搜索内容 -->
			<scroll-view scroll-y="true">
				<!-- 歌曲 -->
				<view class="music-list" v-if="status.search_showMusic">
					<view class="item" v-for="(item,index) in searchList.musicList" :key="index"
						@tap="ToAudioDetails(index)">
						<view class="index">{{index+1}}</view>
						<view class="musicInfo">
							<view class="name">{{item.name}}</view>
							<view class="author"> {{item.author?item.author:'未知歌手'}}</view>
						</view>
						<text class="iconfont" @tap.stop="pullDownMenu(index)">&#xe700;</text>
					</view>
				</view>
				<!-- 歌单 -->
				<view class="album-list" v-else-if="status.search_showAlbum">
					<view class="item" v-for="(item,index) in searchList.albumList" :key="index"
						@tap="ToAlbumDetails(index)">
						<view class="cover">
							<image :src='item.coverPath' mode="widthFix"></image>
							<text>每日更新</text>
						</view>
						<view class="text">
							<view class="name">{{item.name}} ({{item.number}})</view>
							<view class="desc">{{item.desc}}</view>
						</view>
					</view>
				</view>
				<!-- 歌手 -->
				<view class="author" v-else-if="status.search_showAuthor"></view>
				<view class="blank" style="height: 220rpx;"></view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import headBarWX from '@/components/headBarWX/headBarWX.vue'
	import headBarAPP from '@/components/headBarAPP/headBarAPP.vue'
	// 导入云对象
	const cloudMusic = uniCloud.importObject('cloudMusic', {
		customUI: true // 取消自动展示的交互提示界面
	});
	const Album = uniCloud.importObject('Album', {
		customUI: true // 取消自动展示的交互提示界面
	});
	export default {
		data() {
			return {
				searchText: '',
				searchList: {
					musicList: [],
					albumList: [],
					authorList: [],
				},
				status: {
					search_showMusic: true,
					search_showAlbum: false,
					search_showAuthor: false,
				},
			}
		},
		onLoad(options) {
			let name = options.searchText;
			this.searchText = name;
			let musicInfo = {
				name,
			}
			cloudMusic.searchMusic(musicInfo).then(res => {
				this.searchList.musicList = res.data.data;
				console.log(this.searchList.musicList);
			})
		},
		methods: {
			/**搜索云端歌曲
			 * 
			 */
			searchMusic() {
				// 搜索云端歌曲
				let name = this.searchText;
				let musicInfo = {
					name,
				}
				cloudMusic.searchMusic(musicInfo).then(res => {
					this.searchList.musicList = res.data.data;
					console.log(this.searchList.musicList);
				});
			},
			/**搜索云端歌单
			 * 
			 */
			searchAlbum() {
				// 搜索云端歌单
				let name = this.searchText;
				let albumInfo = {
					name,
				}
				Album.searchAlbums(albumInfo).then(res => {
					this.searchList.albumList = res.data.data;
					console.log(this.searchList.albumList);
				});
			},
			/**搜索云端歌手
			 * 
			 */
			searchAuthor() {
				// 搜索云端歌手
				let name = this.searchText;
				let albumInfo = {
					name,
				}
				Album.searchAlbums(albumInfo).then(res => {
					this.searchList.albumList = res.data.data;
					console.log(this.searchList.albumList);
				});
			},
			search() {
				console.log('搜索');

				if (this.status.search_showMusic) {
					this.searchMusic();
				} else if (this.status.search_showAlbum) {
					this.searchAlbum();
				} else if (this.status.search_showAuthor) {

				}
			},
			/**选择 显示搜索歌曲
			 * 
			 */
			search_showMusic() {
				console.log('搜索 显示歌曲');
				this.status.search_showMusic = true;
				this.status.search_showAlbum = false;
				this.status.search_showAuthor = false;

				this.searchMusic();
			},
			/**选择 显示搜索歌单
			 * 
			 */
			search_showAlbum() {
				console.log('搜索 显示歌单');
				this.status.search_showMusic = false;
				this.status.search_showAlbum = true;
				this.status.search_showAuthor = false;

				this.searchAlbum();
			},
			/**选择 显示搜索歌手
			 * 
			 */
			search_showAuthor() {
				console.log('搜索 显示歌手');
				this.status.search_showMusic = false;
				this.status.search_showAlbum = false;
				this.status.search_showAuthor = true;
			},
			/**播放歌曲
			 * 
			 */
			ToAudioDetails(index) {
				console.log('播放歌曲');
				let id = this.searchList.musicList[index]._id;
				uni.navigateTo({
					url: '/pages/musicPlay/musicPlay?musicID=' + id,
				})
			},
			/**下拉菜单
			 * 
			 */
			pullDownMenu() {
				console.log('下拉菜单');
			},

			/**前往 歌单详情
			 * 
			 */
			ToAlbumDetails(index) {
				uni.navigateTo({
					url: '/pages/album/album?albumID=' + this.searchList.albumList[index]._id,
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	// 搜索框
	.searchBox {
		display: flex;
		align-items: center;
		height: 70rpx;
		margin: 30rpx 30rpx 30rpx 30rpx;
		background-color: #f7f7f7;
		border-radius: 50rpx;

		text {
			font-size: $icon-size-m-m;
			margin-right: 26rpx;
			margin-left: 28rpx;
		}

		input {
			font-size: $font-size-m-m;
			flex: 1;
		}
	}

	.btn-category {
		width: 100%;
		height: 70rpx;
		display: flex;
		border-bottom: 3rpx solid rgba(0, 0, 0, 0.5);

		.item {
			flex: 1;
			font-size: $font-size-m-m;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.item.activate {
			border-bottom: 6rpx solid rgba(22, 45, 239, 0.5);
		}
	}

	scroll-view {
		height: 100%;
		width: 100%;

		// 歌曲列表
		.music-list {
			.item {
				display: flex;
				margin: 10rpx 10rpx 20rpx 20rpx;
				align-items: center;
				color: #959595;

				.index {
					display: flex;
					justify-content: center;
					width: 58rpx;
					font-size: $font-size-l-m;
					line-height: 30rpx;
				}

				.musicInfo {
					margin-left: 10rpx;
					flex: 1;

					.name {
						font-size: 28rpx;
						color: black;
						width: 70vw;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}

					.author {
						font-size: 20rpx;
						align-items: center;
						width: 70vw;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}

					.author image {
						width: 32rpx;
						height: 20rpx;
						margin-right: 10rpx;
					}

				}

				text {
					font-size: $icon-size-s-m;
					color: #c7c7c7;
					margin-right: 10rpx;
				}
			}
		}

		// 歌单列表
		.album-list {
			margin: 0 30rpx;

			.item {
				display: flex;
				margin-top: 20rpx;
				background-color: rgba(0, 0, 0, 0.05);
				height: 240rpx;
				border-radius: 30rpx;

				.cover {
					width: 200rpx;
					height: 200rpx;
					position: relative;
					border-radius: 30rpx;
					overflow: hidden;
					margin: auto 0;
					margin-left: 20rpx;

					image {
						width: 100%;
						height: 100%;
					}

					text {
						position: absolute;
						left: 12rpx;
						bottom: 16rpx;
						color: white;
						font-size: 20rpx;
					}
				}

				.text {
					margin-top: 30rpx;
					margin-left: 30rpx;
					margin-right: 20rpx;
					margin-bottom: 20rpx;
					overflow: hidden;
					font-size: 24rpx;
					flex: 1;

					.name {
						margin-left: 10rpx;
						font-size: $font-size-m-m;
						font-weight: 600;
						line-height: 66rpx;
					}

					.desc {
						margin-top: 10rpx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 3;
						overflow: hidden;
						text-overflow: ellipsis;
						font-size: $font-size-s-s;
					}
				}
			}
		}
	}
</style>